<?php
/**
 * Created by PhpStorm.
 * User: mawenhuan
 * Date: 2019-05-23
 * Time: 10:49
 */

namespace App\Models\Meimei\Traits;


use App\Utils\OssUtil;

trait ExplodeTrait
{
    protected $spe = ',';

    /**
     * ---多图处理
     * @return $this
     */
    public function SignOssUrlToExplodeImgs($watermark = false)
    {
        $process = '';
        if($watermark) {
            $process = 'image/watermark,image_c2h1aXlpbmcuanBn,t_60,g_center,voffset_2';
        }

        foreach ($this->explode_imgs as $item) {
            $this->$item = explode($this->spe, $this->$item);

            // 标记
            if (isset($this->$item) && count($this->$item) > 0){
                foreach ($this->$item as $index => $img) {
                    $paths[$index] = OssUtil::fetchGetSignUrl($img, true, $process);
                }
                $this->{$item.'_urls'} = $paths;
            }
        }
        return $this;
    }

    /**
     * ---单图处理
     * @return $this
     */
    public function SignOssUrlToExplodeImg($watermark = false)
    {
        $process = '';
        if($watermark) {
            $process = 'image/watermark,image_c2h1aXlpbmcuanBn,t_60,g_center,voffset_2';
        }

        foreach ($this->explode_img as $item) {
            $this->{$item.'_url'} = OssUtil::fetchGetSignUrl($this->$item, true, $process);
        }
        return $this;
    }

    public function Explode()
    {
        foreach ($this->explode as $item) {
            $this->$item = array_filter(explode($this->spe, $this->$item));
        }
        return $this;
    }

    public function ExplodeImgs()
    {
        foreach ($this->explode as $item) {
            if (isset($this->$item) && count($this->$item) > 0){
                foreach ($this->$item as $index => $img) {
                    $paths[$index] = OssUtil::fetchGetSignUrl($img);
                }
                $this->{$item.'_urls'} = $paths;
            }
        }
        return $this;
    }
}
